package best.driver.android.common.network;


import best.driver.android.Utils.Util;
import best.driver.android.common.app.AppProxyFactory;



import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;

public class BaseRequestFactory {

    public static void configRequest(ApiRequest request, String api, Hashtable<String, Object> params) {
        String url = AppProxyFactory.getProxy().getAppConfig().getApiHostUrl() + api;

        if (params == null) {
            params = new Hashtable<String, Object>();
        }

        if (request.getMethod() == ApiRequest.Method.GET) {
            StringBuilder query = new StringBuilder();

            Enumeration<String> iterator = params.keys();
            while (iterator.hasMoreElements()) {
                String aKey = (String) iterator.nextElement();
                Object aValue = params.get(aKey);
                try {
                    aValue = URLEncoder.encode(String.valueOf(aValue), "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                query.append(aKey).append("=");
                query.append(aValue);
                if (iterator.hasMoreElements()) {
                    query.append("&");
                }
            }
            request.setUrl(url + "?" + query.toString());
        } else {
            request.setUrl(url);
            request.setParams(params);
        }
    }
    
    public static String boolToInt(boolean v) {
		return String.valueOf(v ? 1 : 0);
	}
}
